<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Http\Controllers\Controller;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\MessageBag;

class ArticleController extends Controller
{
    use HasResourceActions;
    use CateTrait;


    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('文章列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Article);

        //倒叙排序
        $grid->model()->orderBy('id', 'desc');

        $grid->id('ID')->sortable();

        $grid->title('标题');

        $grid->column('cate.name','分类');

        $grid->view_times('浏览次数');


        $grid->essence('精华')->display(function($status){
            return $status?'是':'否';
        });
        $grid->recommend('推荐')->display(function($status){
            return $status?'是':'否';
        });
        $grid->show_status('是否展示')->display(function($status){
            return $status?'是':'否';
        });

        $grid->cloumn('存在缩略图')->display(function(){
            return $this->thumb?'是':'否';
        });

        $grid->actions(function ($actions){
            //$actions->disableView();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));

        $show->id('Id');
        $show->cate_id('分类')->as(function () use($show){
            return $show->getModel()->cate->name;
        });
        $show->title('标题');
        $show->description('概要');
        $show->thumb('缩略图')->image();
        $show->author('作者');
        $show->view_times('阅读次数');
        $show->recommend('推荐')->as(function ($value){
            return $value?'是':'否';
        });
        $show->show_status('显示')->as(function ($value){
            return $value?'是':'否';
        });
        $show->essence('精华')->as(function ($value){
            return $value?'是':'否';
        });

        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article);




        $form->tab('内容', function ($form) {
            $form->text('content_file','文档地址');

            $form->simplemde('content','内容');

        })->tab('标题信息', function ($form) {
            $form->display('id', 'ID');
            //获取类的层次结构
            $options=$this->getCateArrange();
            $form->select('cate_id','父级分类')->options($options)->default(-1)->rules('required|numeric|min:0');
            $form->text('title','标题')->rules(function($form){

                if($id=$form->model()->id){
                    return 'required|unique:articles,title,'.$id.',id';
                }else{
                    return 'required|unique:articles';
                }
            });
            $form->text('author','作者')->default((new Admin())->user()->username);

            $form->number('view_times','浏览次数')->default(0);
            $states = [
                'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
            ];
            $form->switch('essence','精华')->states($states)->default(0);
            $form->switch('recommend','推荐')->states($states)->default(1);
            $form->switch('show_status','是否展示')->states($states)->default(1);

            $image=$form->image('thumb','缩略图');
            $image->move('articles');
            $image->uniqueName();

            $form->text('description','概要');


        });


        $form->saved(function (Form $form) {
            //更新缓存
            Cache::forget('article_'. $form->model()->id);//最新文章

            admin_success('success', '保存成功');

            $redirect = app('redirect');

            return $redirect->away($redirect->getUrlGenerator()->to("admin/articles/{$form->model()->id}/edit"));
        });



        return $form;
    }
}
